/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.digitaldiary.validator;

import com.digitaldiary.bean.UserHandler;
import com.digitaldiary.entity.DiaryUser;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Sanjay
 */
@FacesValidator("com.digitaldiary.validator.UserNameValidator")
public class UserNameValidator implements Validator{
  
   	FacesContext context = FacesContext.getCurrentInstance();
	 UserHandler userhandler = (UserHandler) context.getApplication().getExpressionFactory()
         .createValueExpression(context.getELContext(), "#{userHandler}", UserHandler.class)
         .getValue(context.getELContext());
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        
        String username= (String) value;
        DiaryUser user= (DiaryUser) userhandler.checkUser(username);
        if(user!= null){
            
                FacesMessage message= new FacesMessage("This user name already exist.");
                throw new ValidatorException(message);
            }
      
    }
    
}
